package com.aruba.girlanime.delegate;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.daimajia.androidanimations.library.BaseViewAnimator;
import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;

/**
 * 小元素
 * Created by aruba on 2018/4/17.
 */

public class ItemAnimeDelegate implements IAnimeDelegate {
    View iv_green;
    View iv_arrow1;
    View iv_arrow2;
    View iv_yellow;
    View iv_star;

    private boolean isInit;
    private int playDuration = 1000;

    @Override
    public void startAnime() {
        YoYo.with(Techniques.Bounce)
                .interpolate(new LinearInterpolator())
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_green);

        YoYo.with(Techniques.Pulse)
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .pivot(iv_star.getMeasuredWidth(), iv_star.getMeasuredHeight())
                .playOn(iv_star);

        YoYo.with(new RotateDownRightAnimator())
                .interpolate(new LinearInterpolator())
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_yellow);

        YoYo.with(new Arrow1Animator())
                .interpolate(new LinearInterpolator())
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_arrow1);

        YoYo.with(new Arrow1Animator())
                .interpolate(new LinearInterpolator())
                .duration((long) (playDuration * (Math.random() + 1)))
                .repeat(ValueAnimator.INFINITE)
                .playOn(iv_arrow2);
    }

    @Override
    public void init(View... args) {
        iv_green = args[0];
        iv_arrow1 = args[1];
        iv_arrow2 = args[2];
        iv_yellow = args[3];
        iv_star = args[4];

        iv_star.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                if (!isInit) {
                    isInit = true;
                    startAnime();
                }
            }
        });
    }

    @Override
    public void stopAnime() {

    }

    @Override
    public void reset() {

    }


    /**
     * 球的anime
     */
    public class RotateDownRightAnimator extends BaseViewAnimator {
        @Override
        public void prepare(View target) {
            float x = target.getWidth() - target.getPaddingRight();
            float y = target.getHeight() - target.getPaddingBottom();
            getAnimatorAgent().playTogether(
                    ObjectAnimator.ofFloat(target, "rotation", 0, -360),
                    ObjectAnimator.ofFloat(target, "pivotX", x, x),
                    ObjectAnimator.ofFloat(target, "pivotY", y, y)
            );
        }
    }

    /**
     * 箭头的anime
     */
    public class Arrow1Animator extends BaseViewAnimator {
        private int padding = 20;

        @Override
        public void prepare(View target) {
            getAnimatorAgent().playTogether(
                    ObjectAnimator.ofFloat(target, "translationY", -target.getHeight() - padding, ((View) target.getParent()).getHeight() + padding),
                    ObjectAnimator.ofFloat(target, "translationX", -padding, -((View) target.getParent()).getWidth() + padding)
            );
        }
    }
}
